//
//  VistorView.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 20/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit
import SnapKit

// swift 中定义协议
protocol VistorViewDelegate:NSObjectProtocol {
    
    // 点击登录回调
    func loginBtnClick()
   
    // 点击注册回调
    func registerBtnClick()

}

class VistorView: UIView {
    var  delegate:VistorViewDelegate?
    
    
    func setUpVisitorInfo(isHome:Bool , imageName:String,message:String) -> Void {
        iconView.image = UIImage(named:imageName)
        homeIcon.isHidden = !isHome
        messageLab.text = message
        if isHome {
            startAmimation()
        }
    }
    private func startAmimation() -> Void {
        // 创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画属性
        anim.toValue = 2 * Double.pi
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        // 将动画添加到图层上
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 添加子空间
        addSubview(iconView)
        addSubview(homeIcon)
        addSubview(messageLab)
        addSubview(loginBtn)
        addSubview(registerButton)
        
        // 布局子空间
        iconView.snp.makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        homeIcon.snp.makeConstraints { (maker) -> Void in
            maker.center.equalTo(self)
        }
        messageLab.snp.makeConstraints { (maker) -> Void in
            maker.centerX.equalTo(self)
            maker.top.equalTo(iconView.snp.bottom).offset(10)
//            maker.width.equalTo(self.snp.width)
        }
        // 哪个控件（item）的 什么属性（attribute）关系（relatedBy：equal） 另一个控件（toItem）的哪个属性 （attribute） 乘以（multiplier），加上（constant）
        let widthCons = NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 250)
        addConstraint(widthCons);
        
        loginBtn.snp.makeConstraints { (make) -> Void in
            make.right.equalTo(self.snp.centerX).offset(-20)
            make.width.equalTo(100)
            make.height.equalTo(40)
            make.top.equalTo(messageLab.snp.bottom).offset(10)
        }
        registerButton.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp.centerX).offset(10)
            make.width.equalTo(loginBtn.snp.width)
            make.height.equalTo(40)
            make.top.equalTo(loginBtn.snp.top)
        }
        
    
    }
    
    //Swift 推荐自定义一个控件要么纯代码  要么xib  不建议两者一起
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func loginBtnClick() {
        delegate?.loginBtnClick()
    }
    @objc func registerBtnClick() {
        delegate?.registerBtnClick()
    }
   
    // MARK:懒加载控件
    private lazy var iconView:UIImageView = {
        let imgView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imgView
    }()
    private lazy var homeIcon :UIImageView = {
        let imgView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
        
        return imgView
    }()
    private lazy var messageLab :UILabel = {
       let lab = UILabel()
        lab.text = "暂未登录，前往登录"
        lab.textAlignment = NSTextAlignment.center
        lab.numberOfLines = 0
        lab.font = UIFont.systemFont(ofSize: 16)
        return lab
    }()
    
    private lazy var loginBtn:UIButton = {
       let btn = UIButton()
        btn.setTitle("登录", for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setTitleColor(UIColor.orange, for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: UIControlState.normal)
        btn.addTarget(self, action:#selector(loginBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    private lazy var registerButton:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.black, for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: UIControlState.normal)
        btn.addTarget(self, action:#selector(registerBtnClick), for: UIControlEvents.touchUpInside)

        return btn
    }()
}
